package com.example.sls.dao.logistics;

import com.example.sls.pojo.logistics.EquipmentDiscard;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 设备报废记录表(EquipmentDiscard)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-24 08:24:23
 */
public interface EquipmentDiscardDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from equipment_discard where id=#{id}")
  EquipmentDiscard queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param equipmentDiscard 查询条件
   * @param pageSize         分页对象
   * @param offset           偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from equipment_discard where 1=1" +
          " and (id=#{equipmentDiscard.id} or (isnull(#{equipmentDiscard.id}) or #{equipmentDiscard.id}=0))" +
          " and (equipment_id=#{equipmentDiscard.equipmentId} or (isnull(#{equipmentDiscard.equipmentId}) or #{equipmentDiscard.equipmentId}=0))" +
          " and (discard_date=#{equipmentDiscard.discardDate} or (isnull(#{equipmentDiscard.discardDate}) or #{equipmentDiscard.discardDate}=''))" +
          " and (reason=#{equipmentDiscard.reason} or (isnull(#{equipmentDiscard.reason}) or #{equipmentDiscard.reason}=''))" +
          " and (disposal_method=#{equipmentDiscard.disposalMethod} or (isnull(#{equipmentDiscard.disposalMethod}) or #{equipmentDiscard.disposalMethod}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<EquipmentDiscard> queryAllByLimit(EquipmentDiscard equipmentDiscard, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from equipment_discard")
  long count();

  /**
   * 新增数据
   *
   * @param equipmentDiscard 实例对象
   * @return 影响行数
   */
  @Insert("insert into equipment_discard (id,equipment_id,discard_date,reason,disposal_method) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(!isnull(#{equipmentId}),#{equipmentId},null)," +
          "if(!isnull(#{discardDate}),#{discardDate},null)," +
          "if(!isnull(#{reason}),#{reason},null)," +
          "if(!isnull(#{disposalMethod}),#{disposalMethod},null)" +
          ")")
  int insert(EquipmentDiscard equipmentDiscard);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param equipmentDiscard 实例对象
   * @return 影响行数
   */
  @Update("update equipment_discard set " +
          "equipment_id=if(isnull(#{equipmentId}),equipment_id,#{equipmentId})," +
          "discard_date=if(isnull(#{discardDate}),discard_date,#{discardDate})," +
          "reason=if(isnull(#{reason}),reason,#{reason})," +
          "disposal_method=if(isnull(#{disposalMethod}),disposal_method,#{disposalMethod})" +
          " where id=#{id}")
  int update(EquipmentDiscard equipmentDiscard);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from equipment_discard where id=#{id}")
  int deleteById(Integer id);
}

